<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 17:31
 */

namespace app\admin\controller;
use app\admin\model\MsgModel;

class Push extends Base{
    // 消息列表
    public function index(){
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            $where['to_user']=0;
            if (!empty($param['title'])) {
                $where['title'] = ['like', '%' . $param['title'] . '%'];
            }
            if (!empty($param['message'])) {
                $where['message'] = ['like', '%' . $param['message'] . '%'];
            }
            $model = new MsgModel();
            $selectResult = $model->getMsg($where, $offset, $limit);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['addtime']=date('Y-m-d H:i:s',$vo['addtime']);
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            $return['total'] = $model->getAll($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }
    // 添加消息
    public function add()
    {
        if(request()->isPost()){
            $param = input('post.');
            /*
            vendor("jpush.jpush");
            $jpush = new \jpush();
            $flag=$jpush->send();
            */
            $model = new MsgModel();
            $flag = $model->add($param);
            return json(msg($flag['code'], url('push/index'), $flag['msg']));
        }

        return $this->fetch();
    }
    public function del()
    {
        $id = input('param.id');

        $model = new MsgModel();
        $flag = $model->del($id);
        return json(msg($flag['code'], url('push/index'), $flag['msg']));
    }
    private function makeButton($id)
    {
        return [
            '删除' => [
                'auth' => 'push/del',
                'href' => "javascript:msgDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}